﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Marbles.Core.BBS;
using Marbles.Core.Authentication;
using Marbles.Core.Connectors;
using System.Data;

namespace Marbles.Web.Admin.Content
{
    public partial class BBS : ContentBase
    {

        private ForumCollection items;

        protected void Page_Load(object sender, EventArgs e)
        {
            cPnlSites.Visible = Page.AuthenticationProvider.CurrentToken.IsRoot;

            if (!Page.IsPostBack)
            {
                InitialiseForm();
            }

            LoadData();
        }

        private void InitialiseForm()
        {
            cDrpSite.DataSource = new Core.Content.Sites.SiteDefinitionCollection();
            cDrpSite.DataBind();
            cDrpSite.SelectedValue = this.CurrentWeb.SiteId;
            cDrpCategory.DataSource = ForumCollection.GetForumCategories(cDrpSite.SelectedValue);
            cDrpCategory.DataBind();
            cDrpCategory.ClearSelection();
            LoadUsers();
        }

        protected void cDrpSite_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData();
            cDrpCategory.DataSource = ForumCollection.GetForumCategories(cDrpSite.SelectedValue);
            cDrpCategory.DataBind();
            cDrpCategory.ClearSelection();
            LoadUsers();
        }

        private void LoadUsers()
        {
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("SELECT DISTINCT a.username, a.fullname FROM Users a INNER JOIN usergrouplink b ON a.userid = b.userid INNER JOIN usergroups c ON b.groupid = c.groupid WHERE c.editor = 1 OR c.administrator = 1 AND a.siteid = @siteid ORDER BY fullname ASC");
            cmd.AddParameter("@siteid", cDrpSite.SelectedValue);
            cDrpModerator.Items.Clear();
            cDrpModerator.Items.Add(new ListItem("", ""));

            foreach (DataRow dr in dbc.FetchDataTable(cmd).Rows)
            {
                cDrpModerator.Items.Add(new ListItem(dr["fullname"].ToString(), dr["username"].ToString()));
            }            
        }

        private void LoadData()
        {
            items = new ForumCollection(cDrpSite.SelectedValue);
            cRptLstFora.DataSource = items.Categories;
            cRptLstFora.DataBind();

        }

        protected void cRptLstFora_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            KeyValuePair<string, string> key = (KeyValuePair<string, string>)e.Item.DataItem;
            ListView cLstFora = e.Item.FindControl("cLstFora") as ListView;
            cLstFora.DataSource = items.GetForumByCategory(key.Key);
            cLstFora.DataBind();
        }

        protected void Page_Command(object sender, CommandEventArgs e)
        {
            Forum forum = null;
            string forumname = e.CommandArgument.ToString();

            switch (e.CommandName.ToLower())
            {
                case "editcommand":
                    forum = new Forum(cDrpSite.SelectedValue, forumname);
                    cDlgEditFora.Visible = true;
                    cBtnDelete.CommandArgument = forumname;
                    cBtnSave.CommandArgument = forumname;
                    cBtnDelete.Visible = true;
                    cDlgEditFora.Title = "Edit Forum";
                    ClearNewForumDlg();
                    cDrpCategory.SelectedValue = forum.Category;
                    cDrpModerator.SelectedValue = forum.Moderator;
                    cChkModerated.Checked = forum.IsModerated;
                    cChkPublic.Checked = forum.IsPublic;
                    cChkReadonly.Checked = forum.IsReadonly;
                    cTxtDescription.Text = forum.Description;
                    cTxtForumName.Text = forum.ForumName;
                    cTxtTitle.Text = forum.Title;
                    cTxtForumName.Enabled = false;
                    break;
                case "newforum":
                    cDlgEditFora.Title = "New Forum";
                    cDlgEditFora.Visible = true;
                    ClearNewForumDlg();
                    cBtnDelete.CommandArgument = "";
                    cBtnSave.CommandArgument = "";
                    cBtnDelete.Visible = false;
                    break;
                case "save":
                    if (string.IsNullOrEmpty(forumname))
                    {
                        forum = Forum.CreateNew(cDrpSite.SelectedValue, cTxtForumName.Text.Trim());
                    }
                    else
                    {
                        forum = new Forum(cDrpSite.SelectedValue, forumname);
                    }

                    forum.Category = cDrpCategory.SelectedValue;
                    forum.Moderator = cDrpModerator.SelectedValue;
                    forum.IsModerated = cChkModerated.Checked;
                    forum.IsPublic = cChkPublic.Checked;
                    forum.IsReadonly = cChkReadonly.Checked;
                    forum.Description = cTxtDescription.Text;                     
                    forum.Title = cTxtTitle.Text;
                    forum.Update();
                    LoadData();
                    cDlgEditFora.Visible = false;
                    cBtnDelete.CommandArgument = "";
                    cBtnSave.CommandArgument = "";
                    break;
                case "cancel":
                    cBtnDelete.CommandArgument = "";
                    cBtnSave.CommandArgument = "";
                    cDlgEditFora.Visible = false;
                    LoadData();
                    break;
                case "delete":
                    forum = new Forum(cDrpSite.SelectedValue, forumname);
                    forum.Delete();
                    LoadData();
                    cBtnDelete.CommandArgument = "";
                    cBtnSave.CommandArgument = "";
                    cDlgEditFora.Visible = false;
                    break;
            }
        }

        private void ClearNewForumDlg()
        {
            cDrpCategory.ClearSelection();
            cDrpModerator.ClearSelection();
            cChkModerated.Checked = false;
            cChkPublic.Checked = true;
            cChkReadonly.Checked = false;
            cTxtDescription.Text = "";
            cTxtForumName.Text = "";
            cTxtTitle.Text = "";
            cTxtForumName.Enabled = true;
        }
    }
}